/**
 * 
 */
package com.gr.staffpm.widget.dialog.feedback.model;

import org.apache.wicket.IClusterable;


/**
 * Rating model for storing the ratings.
 * @author Graham Rhodes 14 Feb 2011 17:52:45
 */
public class RatingModel implements IClusterable {

    private static final long serialVersionUID = 1L;
    private int nrOfVotes = 0;
    private int sumOfRatings = 0;
    private int rating = 0;
    private String comment = "";

    /**
     * Returns whether the star should be rendered active.
     * 
     * @param star
     *            the number of the star
     * @return true when the star is active
     */
    public boolean isActive(int star) {
        return star < ((int) (rating + 0.5));
    }

    /**
     * Gets the number of cast votes.
     * 
     * @return the number of cast votes.
     */
    public Integer getNrOfVotes() {
        return nrOfVotes;
    }

    /**
     * Adds the vote from the user to the total of votes, and calculates the rating.
     * 
     * @param nrOfStars
     *            the number of stars the user has cast
     */
    public void addRating(int nrOfStars) {
        nrOfVotes++;
        sumOfRatings += nrOfStars;
        rating = sumOfRatings / nrOfVotes;
    }

    /**
     * Gets the rating.
     * 
     * @return the rating
     */
    public int getRating() {
        return rating;
    }

    /**
     * Returns the sum of the ratings.
     * 
     * @return the sum of the ratings.
     */
    public int getSumOfRatings() {
        return sumOfRatings;
    }

    public String getComment() {
        return comment;
    }

    /**
     * @param nrOfVotes
     */
    public void setNrOfVotes(int nrOfVotes) {
        this.nrOfVotes = nrOfVotes;
    }

    /**
     * @param rating
     */
    public void setRating(int rating) {
        this.rating = rating;
    }

    /**
     * @param sumOfRating
     */
    public void setSumOfRatings(int sumOfRating) {
        this.sumOfRatings = sumOfRating;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void reset() {
        this.nrOfVotes = 0;
        this.rating = 0;
        this.sumOfRatings = 0;
        this.comment = "";
    }
}
